Imports ToolKitVB
Imports System
Imports System.Data
Imports System.Text
Imports System.Configuration
Imports System.ComponentModel
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls

<System.Web.UI.ParseChildren(True), _
System.Drawing.ToolboxBitmap(GetType(ToolKitGridView), _
 "Web.UI.WebControls.GridView.ico")> _
Public Class ToolKitGridView
    Inherits GridView
    Implements IToolKit

    Private tblGridView As New Table()
    Private _AddToJavascript As Boolean = False
    Public Property AddToJavascript() As Boolean
        Get
            Return _AddToJavascript
        End Get
        Set(ByVal value As Boolean)
            _AddToJavascript = value
        End Set
    End Property

    <TemplateContainer(GetType(Control))> _
    Public Overloads Overrides ReadOnly Property NamingContainer() As Control
        Get
            Return MyBase.NamingContainer
        End Get
    End Property
    Protected Overrides Sub OnPreRender(ByVal e As System.EventArgs)
        MyBase.OnPreRender(e)
        If _AddToJavascript Then
            DirectCast(Me.Page, Web.UI.ToolKitPage).Javascript.RegisterControl(Me)
        End If
    End Sub
    Protected Overloads Overrides Function CreateChildControls(ByVal dataSource As System.Collections.IEnumerable, ByVal dataBinding As Boolean) As Integer
        Dim numRows As Integer = MyBase.CreateChildControls(dataSource, dataBinding)

        'no data rows created, create empty table if enabled 
        If numRows = 0 AndAlso ShowWhenEmpty Then
            'create table 

            tblGridView.ID = Me.ID

            'convert the exisiting columns into an array and initialize 
            Dim fields As DataControlField() = New DataControlField(Me.Columns.Count - 1) {}
            Me.Columns.CopyTo(fields, 0)

            If Me.ShowHeader Then
                'create a new header row 
                _headerRow2 = MyBase.CreateRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal)

                Me.InitializeRow(_headerRow2, fields)

                tblGridView.Rows.Add(_headerRow2)
            End If

            'create the empty row 
            Dim emptyRow As New GridViewRow(-1, -1, DataControlRowType.EmptyDataRow, DataControlRowState.Normal)

            Dim cell As New TableCell()
            cell.ColumnSpan = Me.Columns.Count
            cell.Width = Unit.Percentage(100)
            If Not [String].IsNullOrEmpty(EmptyDataText) Then
                cell.Controls.Add(New LiteralControl(EmptyDataText))
            End If

            If Me.EmptyDataTemplate IsNot Nothing Then
                EmptyDataTemplate.InstantiateIn(cell)
            End If

            emptyRow.Cells.Add(cell)
            tblGridView.Rows.Add(emptyRow)

            If Me.ShowFooter Then
                'create footer row 
                _footerRow2 = MyBase.CreateRow(-1, -1, DataControlRowType.Footer, DataControlRowState.Normal)

                Me.InitializeRow(_footerRow2, fields)
                tblGridView.Rows.Add(_footerRow2)

                'I've added the below row to raise the rowcreated event so that on 
                'load the footerrow can be populated with the drop downs - Bill 
                MyBase.OnRowCreated(New GridViewRowEventArgs(_footerRow2))
            End If

            Me.Controls.Clear()
            Me.Controls.Add(tblGridView)
        End If

        Return numRows
    End Function

    <Category("Behaviour")> _
    <Themeable(True)> _
    <Bindable(BindableSupport.No)> _
    Public Property ShowWhenEmpty() As Boolean
        Get
            If ViewState("ShowWhenEmpty") Is Nothing Then
                ViewState("ShowWhenEmpty") = False
            End If

            Return CBool(ViewState("ShowWhenEmpty"))
        End Get
        Set(ByVal value As Boolean)
            ViewState("ShowWhenEmpty") = value
        End Set
    End Property
    ''' <summary> 
    ''' Fixes Dudes null references 
    ''' </summary> 
    Protected _footerRow2 As GridViewRow
    Public Overloads Overrides ReadOnly Property FooterRow() As GridViewRow
        Get
            Dim f As GridViewRow = MyBase.FooterRow
            If f IsNot Nothing Then
                Return f
            Else
                Return _footerRow2
            End If
        End Get
    End Property

    Protected _headerRow2 As GridViewRow
    Public Overloads Overrides ReadOnly Property HeaderRow() As GridViewRow
        Get
            Dim h As GridViewRow = MyBase.HeaderRow
            If h IsNot Nothing Then
                Return h
            Else
                Return Me._headerRow2
            End If
        End Get
    End Property
    Public Shared Function TableToString(ByVal tbl As Table) As String
        Dim sbTable As New StringBuilder
        sbTable.Append("<table>")
        For Each row As DataRow In tbl.Rows
            sbTable.Append("<tr>")
            For Each cell As Object In row.ItemArray
                sbTable.Append("<td>").Append(cell.ToString()).Append("</td>")
            Next
            sbTable.Append("</tr>")
        Next
        sbTable.Append("</table>")
        Return sbTable.ToString
    End Function

    Public Overrides Function ToString() As String Implements IToolKit.ToString
        Return TableToString(tblGridView)
    End Function
End Class